#include "EventPoller.h"
#include "GUIManager.h"
#include "Game.h"
#include "OptionsState.h"
#include "PlayState.h"
#include "SoundManager.h"
#include "SplashState.h"
#include "TutorialState.h"

#include "File.h"

void SplashState::LoadButtons()
{
	m_startButt.SetTexPath("Textures/Start.png");
	m_startButt.SetPos(Vec2f(1.49f, 1.2f));
	m_startButt.SetDims(Vec2f(0.5f, 0.3f));
	m_startButt.SetActivate(true);

	m_optionsButt.SetTexPath("Textures/Options.png");
	m_optionsButt.SetPos(Vec2f(1.49f, 0.83f));
	m_optionsButt.SetDims(Vec2f(0.4f, 0.28f));
	m_optionsButt.SetActivate(true);

	m_tutorialButt.SetTexPath("Textures/Tutorial.png");
	m_tutorialButt.SetPos(Vec2f(1.49f, 0.5f));
	m_tutorialButt.SetDims(Vec2f(0.43f, 0.26f));
	m_tutorialButt.SetActivate(true);
}

void SplashState::OnActivated()
{
	TheEventPoller::Instance()->AddHandler(this);

	//  TheSoundManager::Instance()->MusicPlay(1, true);

	//THE CUSTOM CURSOR
	m_custCursor.SetTexPath("Textures/CustomCursor.png");
	m_custCursor.SetDims(Vec2f(0.1f, 0.1f));
	m_custCursor.SetActivate(true);

	//THE BACKGROUND IMAGE
	m_bkgroundImg.SetTexPath("Textures/Catapults Splash Screen.png");
	m_bkgroundImg.SetPos(Vec2f(0.0f, 0.0f));
	m_bkgroundImg.SetDims(Vec2f(2.0f, 2.0f));
	m_bkgroundImg.SetActivate(true); // TRULY IMPORTANT!!!

	//THE BUTTONS
	LoadButtons();
}

void SplashState::OnDeactivated()
{
	TheEventPoller::Instance()->RemoveHandler(this);

	m_custCursor.SetActivate(false);
	m_bkgroundImg.SetActivate(false);

	m_startButt.SetActivate(false);
	m_optionsButt.SetActivate(false);
	m_tutorialButt.SetActivate(false);
}

void SplashState::Draw()
{
	glClearColor(1,1,1,1);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	TheGUIManager::Instance()->Draw();
}

void SplashState::Update()
{
	TheGUIManager::Instance()->Update();

	m_startButt.SetPressed(false);
	m_optionsButt.SetPressed(false);
	m_tutorialButt.SetPressed(false);
}

void SplashState::OnKeyboardEvent(const SDL_KeyboardEvent& k)
{
	if(k.keysym.sym == SDLK_ESCAPE && k.state == SDL_PRESSED)
	{
		exit(0);
	}
}

void SplashState::OnMouseButtonDownEvent(const SDL_MouseButtonEvent& mb)
{
 	m_startButt.OnMouseButtonDownEvent(mb);
 	m_optionsButt.OnMouseButtonDownEvent(mb);
 	m_tutorialButt.OnMouseButtonDownEvent(mb);

	if(mb.button == SDL_BUTTON_LEFT)
	{
		if(mb.state == SDL_PRESSED)
		{
			if (m_startButt.GetPressed())
			{
				TheGame::Instance()->SetGameState(ThePlayState::Instance());
			}
			else if (m_optionsButt.GetPressed())
			{
				TheGame::Instance()->SetGameState(TheOptionsState::Instance());
			}
			else if (m_tutorialButt.GetPressed())
			{
				TheGame::Instance()->SetGameState(TheTutorialState::Instance());
			}
		}
		else if(mb.state == SDL_RELEASED)
		{

		}
	}
}

void SplashState::OnMouseMotionEvent(const SDL_MouseMotionEvent& mm)
{
 	m_startButt.OnMouseMotionEvent(mm);
 	m_optionsButt.OnMouseMotionEvent(mm);
 	m_tutorialButt.OnMouseMotionEvent(mm);
 	m_custCursor.OnMouseMotionEvent(mm);

	if (m_startButt.GetOver())
	{
		m_startButt.SetTexPath("Textures/Start Glow.png");
 	}
	else if (m_optionsButt.GetOver())
	{
		m_optionsButt.SetTexPath("Textures/Options Glow.png");
	}
	else if (m_tutorialButt.GetOver())
	{
		m_tutorialButt.SetTexPath("Textures/Tutorial Glow.png");
	}
	else
	{
		m_startButt.SetTexPath("Textures/Start.png");
		m_optionsButt.SetTexPath("Textures/Options.png");
		m_tutorialButt.SetTexPath("Textures/Tutorial.png");
	}
}